package com.xadmin.designing;

import com.xadmin.sys.rbac.mapper.UserMapper;
import com.xadmin.sys.rbac.pojo.User;
import com.xadmin.sys.security.config.JWTConfig;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

public class Utls {
    @Resource
    private UserMapper userMapper;
    /**
     * 1、通过工具类获取当前请求用户信息
     * 2、通过网络发送 get 请求 /user/getUserInfo
     */
    public User getUserInfo(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String tokenHeader = request.getHeader(JWTConfig.tokenHeader);
            String token = tokenHeader.replace(JWTConfig.tokenPrefix, "");
            // 解析JWT
            Claims claims = Jwts.parser()
                    .setSigningKey(JWTConfig.secret)
                    .parseClaimsJws(token)
                    .getBody();
            // 获取用户名
            String username = claims.getSubject();
            String userId=claims.getId();
            // 根据id查询用户信息
            User user = new User();
            userMapper.getByName(username);
            return user;
    }

    /**
     * 加密工具
     */
    public void printDePwd(){
        System.out.println(new BCryptPasswordEncoder().encode("123456"));
    }
}
